package com.kmproject.haifu.mvp.ui.widget

import android.animation.Animator
import android.animation.ObjectAnimator
import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.LinearLayout
import com.kmproject.haifu.R
import kotlinx.android.synthetic.main.layout_bottom_bar.view.*
import kotlin.collections.MutableList as MutableList1


/**
 * Description:底部导航栏
 * Created by Kevin.Lee on 2019-08-05.
 */
class HomeBottomBar(context: Context?, attrs: AttributeSet?) : LinearLayout(context, attrs) {
    // Tab选项卡的图片
    private var tabImg: IntArray? = null
    // Tab选项卡的图片（选中）
    private var tabImgSelected: IntArray? = null
    private var centerDrawableRes: Int = 0
    private var selectedTextColor: Int = 0
    private var unSelectedTextColor: Int = 0
    private var mOnBottomBarClick: OnBottomBarClick? = null

    private lateinit var mViewPager: ViewPager
    private var addIsOPen = false
    private var selectedIndex = 0

    init {
        LayoutInflater.from(context).inflate(R.layout.layout_bottom_bar, this, true)
        //获取控件id
//        initId()
        initData()
        onBottomBarClick()
        isSelectedItem()
    }

    private fun initData() {
        tabImg = intArrayOf(
                R.drawable.house_bottom_normal_sy,
                R.drawable.house_bottom_normal_fx,
                R.drawable.house_bottom_normal_xx,
                R.drawable.house_bottom_normal_wd
        )
        tabImgSelected =
                intArrayOf(
                        R.drawable.house_bottom_select_sy,
                        R.drawable.house_bottom_select_fx,
                        R.drawable.house_bottom_select_xx,
                        R.drawable.house_bottom_select_wd
                )
        selectedTextColor = ContextCompat.getColor(context, R.color.color_191919)
        unSelectedTextColor = ContextCompat.getColor(context, R.color.color_191919)
    }

    /**
     * 设置选中下标
     */
    fun setSelectedIndex(index: Int): HomeBottomBar {
        selectedIndex = index
        return this
    }

    /**
     * 设置点击监听器
     */
    fun setOnBottomBarOnclick(onBottomBarClick: OnBottomBarClick): HomeBottomBar {
        mOnBottomBarClick = onBottomBarClick
        return this
    }

    /**
     * 设置选中时图标
     */
    fun setSelectedDrawable(drawableRes: IntArray): HomeBottomBar {
        tabImgSelected = drawableRes
        return this
    }

    /**
     * 设置未选中时图标
     */
    fun setUnSelectedDrawable(drawableRes: IntArray): HomeBottomBar {
        tabImg = drawableRes
        return this
    }

    /**
     * 设置中间按钮图标
     */
    fun setCenterDrawable(drawableRes: Int): HomeBottomBar {
        centerDrawableRes = drawableRes
        return this
    }

    /**
     * 设置选中文字颜色
     */
    fun setSelectedTextColor(color: Int): HomeBottomBar {
        this.selectedTextColor = ContextCompat.getColor(context, color)
        return this
    }

    /**
     * 设置未选中文字颜色
     */
    fun setUnSelectedTextColor(color: Int): HomeBottomBar {
        this.unSelectedTextColor = ContextCompat.getColor(context, color)
        return this
    }

    /**
     * 调用各种set方法之后，调用refreshView()就能即使显示效果。
     */
    fun refreshView() {
        isSelectedItem()
    }

    fun startAddAnim() {
        val rotation: ObjectAnimator
        if (addIsOPen) {
            val startX = imv_bottom_center.translationX
            val startY = imv_bottom_center.translationY

            Log.d("Tag", "startX=$startX startY =$startY")

            //点击按钮的旋转动画
            rotation = ObjectAnimator.ofFloat(imv_bottom_center, "rotation", 90f, 0f)
            rotation.addListener(object : Animator.AnimatorListener {
                override fun onAnimationRepeat(animation: Animator?) {
                }

                override fun onAnimationEnd(animation: Animator?) {
                    imv_bottom_center.setImageResource(R.drawable.house_bottom_normal_fb)
                    addIsOPen = !addIsOPen
                }

                override fun onAnimationCancel(animation: Animator?) {
                }

                override fun onAnimationStart(animation: Animator?) {
                }
            })
        } else {
            val startX = imv_bottom_center.translationX
            val startY = imv_bottom_center.translationY

            Log.d("Tag", "startX=$startX startY =$startY")

            //点击按钮的旋转动画
            rotation = ObjectAnimator.ofFloat(imv_bottom_center!!, "rotation", 0f, 90f)
            rotation.addListener(object : Animator.AnimatorListener {
                override fun onAnimationRepeat(animation: Animator?) {
                }

                override fun onAnimationEnd(animation: Animator?) {
                    imv_bottom_center.setImageResource(R.drawable.house_bottom_normal_fb)
                    addIsOPen = !addIsOPen
                }

                override fun onAnimationCancel(animation: Animator?) {
                }

                override fun onAnimationStart(animation: Animator?) {
                }
            })
        }

        rotation.duration = 500
        rotation.start()
    }

    /**
     * 底部按钮点击监听器
     */
    private fun onBottomBarClick() {
        ll_bottom_layout_1.setOnClickListener {
            selectedIndex = 0
            isSelectedItem()
            mViewPager.currentItem = 0
            mViewPager.adapter?.notifyDataSetChanged()
            if (mOnBottomBarClick != null) {
                mOnBottomBarClick!!.onClickItem(0)
            }
        }
        ll_bottom_layout_2.setOnClickListener {
            selectedIndex = 1
            isSelectedItem()
            mViewPager.currentItem = 1
            mViewPager.adapter?.notifyDataSetChanged()
            if (mOnBottomBarClick != null) {
                mOnBottomBarClick!!.onClickItem(1)
            }
        }
        ll_bottom_layout_3.setOnClickListener {
            selectedIndex = 2
            isSelectedItem()
            mViewPager.currentItem = 2
            mViewPager.adapter?.notifyDataSetChanged()
            if (mOnBottomBarClick != null) {
                mOnBottomBarClick!!.onClickItem(2)
            }
        }
        ll_bottom_layout_4.setOnClickListener {
            selectedIndex = 3
            isSelectedItem()
            mViewPager.currentItem = 3
            mViewPager.adapter?.notifyDataSetChanged()
            if (mOnBottomBarClick != null) {
                mOnBottomBarClick!!.onClickItem(3)
            }
        }
        fl_bottom_center.setOnClickListener {
            startAddAnim()
            if (mOnBottomBarClick != null) {
                mOnBottomBarClick!!.onCenterClick()
            }
        }

    }

    fun onAttach(vp: ViewPager): HomeBottomBar {
        if (vp.adapter?.count != 4) {
            throw RuntimeException("The number of fragment must be 4.")
        }
        if (vp.adapter == null) {
            throw RuntimeException("Please call the setAdapter method before this.")
        }

        mViewPager = vp
        mViewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(p0: Int) {
            }

            override fun onPageScrolled(p0: Int, p1: Float, p2: Int) {
            }

            override fun onPageSelected(p0: Int) {
                selectedIndex = p0
                isSelectedItem()
            }
        })
        return this
    }

    private fun isSelectedItem() {
        when (selectedIndex) {
            0 -> {
                imv_bottom_icon_1.setImageResource(tabImgSelected!![0])
                imv_bottom_icon_2.setImageResource(tabImg!![1])
                imv_bottom_icon_3.setImageResource(tabImg!![2])
                imv_bottom_icon_4.setImageResource(tabImg!![3])

                tv_bottom_text_1.setTextColor(selectedTextColor)
                tv_bottom_text_2.setTextColor(unSelectedTextColor)
                tv_bottom_text_3.setTextColor(unSelectedTextColor)
                tv_bottom_text_4.setTextColor(unSelectedTextColor)
            }
            1 -> {
                imv_bottom_icon_1.setImageResource(tabImg!![0])
                imv_bottom_icon_2.setImageResource(tabImgSelected!![1])
                imv_bottom_icon_3.setImageResource(tabImg!![2])
                imv_bottom_icon_4.setImageResource(tabImg!![3])

                tv_bottom_text_1.setTextColor(unSelectedTextColor)
                tv_bottom_text_2.setTextColor(selectedTextColor)
                tv_bottom_text_3.setTextColor(unSelectedTextColor)
                tv_bottom_text_4.setTextColor(unSelectedTextColor)
            }
            2 -> {
                imv_bottom_icon_1.setImageResource(tabImg!![0])
                imv_bottom_icon_2.setImageResource(tabImg!![1])
                imv_bottom_icon_3.setImageResource(tabImgSelected!![2])
                imv_bottom_icon_4.setImageResource(tabImg!![3])

                tv_bottom_text_1.setTextColor(unSelectedTextColor)
                tv_bottom_text_2.setTextColor(unSelectedTextColor)
                tv_bottom_text_3.setTextColor(selectedTextColor)
                tv_bottom_text_4.setTextColor(unSelectedTextColor)
            }
            3 -> {
                imv_bottom_icon_1.setImageResource(tabImg!![0])
                imv_bottom_icon_2.setImageResource(tabImg!![1])
                imv_bottom_icon_3.setImageResource(tabImg!![2])
                imv_bottom_icon_4.setImageResource(tabImgSelected!![3])

                tv_bottom_text_1.setTextColor(unSelectedTextColor)
                tv_bottom_text_2.setTextColor(unSelectedTextColor)
                tv_bottom_text_3.setTextColor(unSelectedTextColor)
                tv_bottom_text_4.setTextColor(selectedTextColor)
            }
        }
    }

    interface OnBottomBarClick {
        /**
         * 点击底部切换视图的
         * @param index 点击下标；{0-第一个；1-第二个；2-第三个；3-第四个}
         */
        fun onClickItem(index: Int) {}

        /**
         * 点击中间按钮的回调
         */
        fun onCenterClick()
    }
}